//
//  YHYAddStudyViewController.m
//  Home
//
//  Created by 杨泓晔 on 16/4/25.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYAddStudyViewController.h"
#import "YHYDesignerPersonViewController.h"
#import "YHYDesignerPTableViewCell.h"
#import "WSCustomPickerView.h"
@interface YHYAddStudyViewController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,WSCustomPickerViewDelegate>
//@property (strong, nonatomic) IBOutlet UITextField *workName;
@property (strong, nonatomic) IBOutlet UIButton *saveButton;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic,strong)UITextField *myTextfield;

@property (nonatomic,strong) UITextView *textV;
@property (nonatomic,strong) UILabel *uilabel;
@property (nonatomic,strong) UILabel *numLabel;

@property (nonatomic, retain) UITextField *nameTextField;
//@property (nonatomic, retain) UILabel *numLabel;
@property (nonatomic, assign) BOOL isSelect;
//@property (nonatomic, retain) UILabel *uilabel;
@property (nonatomic, assign) NSInteger intA;
@property (nonatomic, assign) NSInteger numChiese;
@property (nonatomic,retain) UIButton *button;
//选择器
@property (nonatomic, strong) WSCustomPickerView *customPicker;
@end

@implementation YHYAddStudyViewController
- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    self.title = @"教育经历";
    
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LV;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    // Do any additional setup after loading the view from its nib.
    _myTableView  = [UITableView new];
    _array = [NSMutableArray arrayWithObjects:@"学校" ,@"专业",@"学历",@"时间段", nil];
    [self.view addSubview:_myTableView];
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view);
        
        make.height.mas_equalTo(ROWHEIGHT * AUTO_HEIGHT * 5);
    }];
    _myTableView.scrollEnabled = NO;
    _myTableView.rowHeight = ROWHEIGHT * AUTO_HEIGHT;
    _myTableView.delegate = self;
    _myTableView.dataSource  = self;
    self.myTableView.backgroundColor = LV;
    UILabel *footL = [UILabel new];
    
    footL.text = @"在校经历";
    footL.textColor = LINE;
    footL.font = [UIFont fontWithName:TFont size:TEXTCONTENTFONT * AUTO_WIDTH];
    
    _myTableView.tableFooterView = footL;
    
    footL.frame = CGRectMake(15 * AUTO_WIDTH, 0, SCREEN_WIDTH, 60 * AUTO_HEIGHT);
    
    [_myTableView registerClass:[YHYDesignerPTableViewCell class] forCellReuseIdentifier:@"YHYDesignerPTableViewCell"];
    
    _myTextfield = [UITextField new];
    [self.view addSubview:_myTextfield];
    [_myTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.myTableView.tableFooterView.mas_bottom);
        make.left.width.mas_equalTo(self.view);
        make.height.mas_equalTo(200 * AUTO_HEIGHT);
        
    }];
    
    
    //bigView
    self.textV = [UITextView new];
    [self.view addSubview:self.textV];
    self.textV.backgroundColor = [UIColor whiteColor];
    [self.textV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.myTableView.mas_bottom).offset(0 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.height.mas_equalTo(150 * AUTO_HEIGHT);
        
    }];
    self.textV.returnKeyType = UIReturnKeyDefault;//返回键的类型
    self.textV.delegate = self;
    self.textV.font = [UIFont systemFontOfSize:TEXTCONTENTFONT*TEXTWEIGHT];
    self.textV.keyboardType = UIKeyboardTypeDefault;//键盘类型
    self.textV.userInteractionEnabled = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.uilabel = [UILabel new];
    
    [self.textV addSubview:self.uilabel];
    self.uilabel.text = @"作为班级团支书主要负责团员，党员的学习工作";
    
    self.uilabel.enabled = NO;//lable必须设置为不可用
    self.uilabel.font = [UIFont systemFontOfSize:TEXTLABELFONT*TEXTWEIGHT];
    self.uilabel.frame = CGRectMake(10, 10 * AUTO_HEIGHT, self.view.frame.size.width - 20 * AUTO_WIDTH, 12 * AUTO_HEIGHT);
    //
    self.numLabel = [UILabel new];
    [self.view addSubview:self.numLabel];
    self.numLabel.textColor = [UIColor blackColor];
    self.numLabel.alpha = 0.6;
    self.numLabel.font = [UIFont systemFontOfSize:TEXTLABELFONT*TEXTWEIGHT];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textV.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view.mas_right).offset( - 50  * AUTO_WIDTH);
        make.height.mas_equalTo(10 * AUTO_WIDTH);
        
    }];
    
    self.numLabel.text = @"0/100";
    
    
    
    UITapGestureRecognizer *tap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.backgroundColor = GH;
    self.button.layer.masksToBounds = YES;
    self.button.layer.cornerRadius = 5;
    [self.button setTitle:@"保存" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    // [self.button setTitleColor:HS forState:UIControlStateNormal];
    self.button.frame = CGRectMake(44.00/2*AUTO_WIDTH,470*AUTO_WIDTH, self.view.frame.size.width -88/2*AUTO_WIDTH, 78/2*AUTO_WIDTH);
    self.button.titleLabel.font = [UIFont fontWithName:@"Arial" size:LOGINFONT * TEXTWEIGHT];
    [self.view addSubview:_button];
    
    //    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.mas_equalTo(self.textV.mas_bottom).offset(118/3 * AUTO_HEIGHT);
    //        make.center.equalTo(self);
    //        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 100 * AUTO_WIDTH, 40 * AUTO_HEIGHT));
    //    }];
    
    
    
    
    
}
-(void)viewTapped:(UITapGestureRecognizer*)tap



{
    [_textV resignFirstResponder];
    
}

//判断是否为汉字
- (BOOL)isChinesecharacter:(NSString *)string{
    if (string.length == 0) {
        return NO;
    }
    unichar c = [string characterAtIndex:0];
    if (c >=0x4E00 && c <=0x9FA5)
    {         return YES;
        //汉字
    }     else
    {         return NO;
        //英文
    }
}
//计算汉字的个数
- (NSInteger)chineseCountOfString:(NSString *)string{
    int ChineseCount = 0;
    if (string.length == 0) {
        return 0;
    }
    for (int i = 0; i<string.length; i++) {
        unichar c = [string characterAtIndex:i];
        if (c >=0x4E00 && c <=0x9FA5)
        {
            ChineseCount++ ;
            //汉字
        }
    }    return ChineseCount;
}
//计算字母的个数
- (NSInteger)characterCountOfString:(NSString *)string{
    int characterCount = 0;
    if (string.length == 0)
    {        return 0;
    }
    for (int i = 0; i<string.length; i++) {
        unichar c = [string characterAtIndex:i];
        if (c >=0x4E00 && c <=0x9FA5)
        {
            
        }
        else
            
        {            characterCount++;
            //英文
        }
    }
    return characterCount;
}



-(void)textViewDidChange:(UITextView *)textView
{
    self.textV.text = textView.text;
    if (textView.text.length == 0) {
        self.uilabel.text = @"一句话介绍自己，和大家打声招呼，写写现在的心情";
    }else{
        self.uilabel.text = @"";
    }
    
    self.intA =[self characterCountOfString:self.textV.text];
    self.numChiese = [self chineseCountOfString:self.textV.text];
    self.numLabel.text = [NSString stringWithFormat:@"%ld/100",(self.numChiese + self.intA)];
    
    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"YHYDesignerPTableViewCell";
    YHYDesignerPTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    cell.xmLabel.text = _array[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
       
    }
    NSLog(@"dsad");
}
- (IBAction)save:(UIButton *)sender {
    NSString *str = _textV.text;
    
    [self.navigationController popViewControllerAnimated:YES];
    
    // [self.delegate presentworkName:str];
   // [self.delegate presentAddWorkName:str];
    [self.delegate presentStudy:str];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)Back:(UIButton *) goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
